package view;

import java.io.InputStream;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import view.listener.CloseButtonListener;
import view.listener.StartButtonListener;
import control.Storage;
import control.Util;

public class ConfigFrame {

	public static final String[] ITEMS = { "2 X 2", "4 X 4", "6 X 6" };
	private Display display;
	private Shell shell;
	private Label playerName, enemyName;
	private Text playerNameField, enemyNameField;
	private Button start, close;
	private Label sizeName;
	private Combo sizeNameCombo;
	private  ResourceBundle messages;
	
	public ConfigFrame() {

		display = Display.getCurrent() != null ? Display.getCurrent() : new Display();
		shell = new Shell(display);
		this.messages = Storage.getInstance().getMessages();
		configureShell();
		keepAlive();
	}

	private void buildContent() {

		setIcons();
		playerName = new Label(shell, SWT.NULL);
		playerName.setText(messages.getString("PLAYERNAMELABEL"));

		playerNameField = new Text(shell, SWT.BORDER);
		playerNameField.setLayoutData(new GridData(100, 15));

		enemyName = new Label(shell, SWT.NULL);
		enemyName.setText(messages.getString("ENEMYNAMELABEL"));

		enemyNameField = new Text(shell, SWT.BORDER);
		enemyNameField.setLayoutData(new GridData(100, 15));

		sizeName = new Label(shell, SWT.NULL);
		sizeName.setText(messages.getString("SIZELABEL"));

		sizeNameCombo = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
		sizeNameCombo.setLayoutData(new GridData(100, 15));
		sizeNameCombo.setItems(ITEMS);
		start = new Button(shell, SWT.PUSH);
		start.setText(messages.getString("STARTBUTTONLABEL"));
		start.addSelectionListener(new StartButtonListener());

		close = new Button(shell, SWT.PUSH);
		close.setText(messages.getString("CLOSEBUTTONLABEL"));
		close.addSelectionListener(new CloseButtonListener(shell));

	}

	private void configureShell() {
		shell.setLayout(getShellLayout());
		shell.setText(messages.getString("SHELLTITLE"));
		buildContent();
		shell.pack();
		Util.centerFrame(shell);
		shell.setVisible(true);
		shell.setFocus();
	}
	private void setIcons(){
		
		InputStream in3 = this.getClass().getResourceAsStream("/res/card_16.png");
		final Image small = new Image(display, in3);
		InputStream in4 = this.getClass().getResourceAsStream("/res/card_32.png");
		final Image large = new Image(display, in4);
		final Image[] images = new Image[] { small, large };
		shell.setImages(images);
	}
	

	private Layout getShellLayout() {
		GridLayout layout = new GridLayout(2, false);
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		return layout;
	}

	private void keepAlive() {

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}
}
